﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;

namespace DR5.SPExtend
{
    public static class SPListExtension
    {
        /// <summary>
        /// Retrives the ListItem from list using CAML query.
        /// </summary>
        /// <param name="currentList"></param>
        /// <param name="fldName">Field Name or Internal Name</param>
        /// <param name="fldValue">Text value of field</param>
        /// <param name="isRecursive">Check sub folders</param>
        /// <returns></returns>
        public static SPListItem GetListItem(this SPList currentList, string fldName, string fldValue, bool isFieldInternalname, bool isRecursive = false)
        {
            SPListItem returnValue = null;
            SPListItemCollection listItems = null;
            SPQuery query = new SPQuery();

            if (!isFieldInternalname)
            {
                fldName = currentList.Fields.GetField(fldName).InternalName;
            }

            query.Query = string.Format("<Where><Eq><FieldRef Name='{0}'/><Value Type='Text'>{1}</Value></Eq></Where>", fldName, fldValue);
            if (isRecursive)
            {
                query.ViewAttributes = "Scope=\"Recursive\"";
            }

            listItems = currentList.GetItems(query);
            if (listItems != null)
            {
                if (listItems.Count > 0)
                {
                    returnValue = listItems[0];
                }
            }
            return returnValue;
        }

        public static string GetListItem(this SPList currentList, string fldNameToCompare, string fldType, string fldValueToCompare,
            string viewfldName, bool isRecursive = false)
        {
            SPListItem returnValue = null;
            SPListItemCollection listItems = null;
            SPQuery query = new SPQuery();

            query.Query = string.Format("<Where><Eq><FieldRef Name='{0}'/><Value Type='{1}'>{2}</Value></Eq></Where>", fldNameToCompare, fldType, fldValueToCompare);
            query.ViewFields = string.Format("<FieldRef Name=\"{0}\"/>", viewfldName);
            if (isRecursive)
            {
                query.ViewAttributes = "Scope=\"Recursive\"";
            }

            listItems = currentList.GetItems(query);
            if (listItems.Count > 0)
            {
                returnValue = listItems[0];
            }
            return returnValue[0].ToString();
        }

        public static void EnsureHasUniqueRoleAssignments(this SPList list)
        {
            if (!list.HasUniqueRoleAssignments)
            {
                list.BreakRoleInheritance(true);
            }
        }

        public static SPFolder EnsureFolder(this SPList list, string folderName)
        {
            SPFolder folder = null;
            bool folderExist = list.RootFolder.SubFolders.FolderExist(folderName);
            if (folderExist)
            {
                folder = list.RootFolder.SubFolders[folderName];
            }
            else
            {
                list.ParentWeb.AllowUnsafeUpdates = true;
                folder = list.RootFolder.SubFolders.Add(folderName);
                list.ParentWeb.AllowUnsafeUpdates = false;
                //folder.Update();
            }
            return folder;
        }

        public static bool FolderExist(this SPFolderCollection usrColl, string folderName)
        {
            foreach (SPFolder folder in usrColl)
            {
                if (string.Compare(folder.Name, folderName, StringComparison.OrdinalIgnoreCase) == 0)
                {
                    return true;
                }
            }
            return false;
        }
    }
}
